package com.wjb.xietong.ota;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;

import com.wjb.xietong.R;
import com.wjb.xietong.component.ActManager;
import com.wjb.xietong.component.AppGlobal;
import com.wjb.xietong.ota.tab1.FMTab1;
import com.wjb.xietong.ota.tab2.FMTab2;
import com.wjb.xietong.ota.tab3.FMTab3;
import com.wjb.xietong.ota.tab4.FMSetting;
import com.wjb.xietong.server.login.LoginResponse;
import com.wjb.xietong.util.UpdateManager;

public class OTAMainMenu extends FragmentActivity {

	private FragmentTabHost mTabHost;
	private RadioGroup m_radioGroup;
	String tabs[] = { "Tab1", "Tab2", "Tab3", "TabCRM", "Tab4" };
	Class<?> cls[] = { FMTab1.class, FMTab2.class, FMTab3.class,
			TabCrm.class, FMSetting.class };
	private AppGlobal app_ = null;
	private int curTabIndex = 0;
	private long firstTime = 0;
	
	private ImageView redPot;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.widget_tabs);
		app_ = (AppGlobal) getApplication();
		app_.setActManager(ActManager.Instance());
		app_.getActManager().pushActivity(this);
		init();
		
		int ret = UpdateManager.compare(AppGlobal.getInstance().getLocalAppVersion(),
				LoginResponse.instance().getNewVersion());
		if (ret < 0){
			// have new version
			showUpdateDiag();
		}
		else{
			// do nothing
		}
	}

	private void showUpdateDiag(){
		AlertDialog.Builder builder = new Builder(AppGlobal
				.getInstance().getContext());  
        builder.setTitle(R.string.soft_new_version);  
        builder.setMessage(LoginResponse.instance().getVersionDesc());  
        // 更新  
        builder.setPositiveButton(R.string.soft_update_updatebtn, new OnClickListener()  
        {  
            @Override  
            public void onClick(DialogInterface dialog, int which)  
            {  
                dialog.dismiss();  
                // 显示下载对话框  
        		Intent intent = new Intent(Intent.ACTION_VIEW);
        		intent.setData(Uri.parse(getString(R.string.new_version_update_url)));
        		startActivity(intent);
            }  
        });  
        // 稍后更新  
        builder.setNegativeButton(R.string.soft_update_later, new OnClickListener()  
        {  
            @Override  
            public void onClick(DialogInterface dialog, int which)  
            {  
                dialog.dismiss();  
            }  
        });  
        Dialog noticeDialog = builder.create();  
        noticeDialog.show();  
	}
	
	private void init() {
		redPot = (ImageView) this.findViewById(R.id.id_red_pot);
		
		mTabHost = (FragmentTabHost) this.findViewById(android.R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
		mTabHost.getTabWidget().setVisibility(View.GONE);
		for (int i = 0; i < tabs.length; i++) {
			mTabHost.addTab(mTabHost.newTabSpec(tabs[i]).setIndicator(tabs[i]),
					cls[i], null);
		}
		m_radioGroup = (RadioGroup) findViewById(R.id.main_radiogroup);
		m_radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.RadioButton0:
					curTabIndex = 0;
					mTabHost.setCurrentTabByTag(tabs[0]);
					if(redPot.isShown()){
						redPot.setVisibility(View.INVISIBLE);
					}
					break;
				case R.id.RadioButton1:
					curTabIndex = 1;
					mTabHost.setCurrentTabByTag(tabs[1]);
					break;
				case R.id.RadioButton2:
					curTabIndex = 2;
					mTabHost.setCurrentTabByTag(tabs[2]);
					break;
				case R.id.RB_CRM:
					curTabIndex = 3;
					mTabHost.setCurrentTabByTag(tabs[3]);
					break;
				case R.id.RadioButton3:
					curTabIndex = 4;
					mTabHost.setCurrentTabByTag(tabs[4]);
					break;	
				}
			}
		});

		((RadioButton) m_radioGroup.getChildAt(0)).toggle();
		
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (curTabIndex == 0) { // 如果是在第一个tab页
				long secondTime = System.currentTimeMillis();
				if (secondTime - firstTime > 1500) {// 如果两次按键时间间隔大于1500毫秒，则不退出
					Toast.makeText(OTAMainMenu.this,
							getString(R.string.STR_COMMON_11),
							Toast.LENGTH_SHORT).show();
					firstTime = secondTime;// 更新firstTime
					return true;
				} else {
					app_.getActManager().popAllActivity();
				}
			} else {
				mTabHost.setCurrentTabByTag(tabs[0]); // 跳转至第一个tab页
				((RadioButton) m_radioGroup.getChildAt(0)).toggle();
			}

			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	public void noticeRedPot(){
		if(curTabIndex == 0){
			if(redPot.isShown()){
				redPot.setVisibility(View.INVISIBLE);
			}
		}else{
			if(!redPot.isShown()){
				redPot.setVisibility(View.VISIBLE);
			}
		}
	}
}
